//
//  LoginVC.m
//  MoreStarLiveTelecast
//
//  Created by babbage on 16/8/21.
//  Copyright © 2016年 babbage. All rights reserved.
//

#import "LoginVC.h"
#import "NavView.h"                             //导航栏
#import "UMSocial.h"                            //友盟
#import "RegistVC.h"                            //注册
#import "FindPWD.h"                             //找回密码


static CGFloat const TextFieldLoginHeight = 50;//登录注册的高度

@interface LoginVC ()<UITextFieldDelegate>

@property (nonatomic, strong) NavView *navView;//导航栏

@property (nonatomic, strong) UITextField *txtUser;//用户手机号码

@property (nonatomic, strong) UITextField *txtPwd;//用户密码

@property (nonatomic, strong) UIButton *login;//登录按钮

@property (nonatomic, strong) NSString *registTimerStr;//倒计时时间
@property (nonatomic, strong) NSString *findPwdTimerStr;//倒计时时间

@end

@implementation LoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BackGroundColor;
    
    [self addNotification];//为了跳转下一页面更流畅的展示倒计时
    [self createSta];//状态栏
    [self createNav];//导航栏
    [self createUserAndPwd];//用户名和密码
    [self createLoginBtn];//登录按钮
    [self createThirdLogin];//第三方登录
}


#pragma mark --- 点击事件 ---
-(void)userWantLogin
{
    [self.view endEditing:YES];
    MJRefreshLog(@"PhoneLogin");
}
-(void)findPwd
{
    MJRefreshLog(@"findPwd");
    FindPWD *findPWD = [[FindPWD alloc] init];
    findPWD.findPWDTimer = self.findPwdTimerStr;
    [self presentViewController:findPWD animated:YES completion:^{
        
    }];
}
-(void)QQLogin
{
    MJRefreshLog(@"你点击了QQ登录");
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
    [self loginWithType:snsPlatform];
}
-(void)WXLogin
{
    MJRefreshLog(@"你点击了WX登录");
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
    [self loginWithType:snsPlatform];
}
-(void)WBLogin
{
    MJRefreshLog(@"你点击了WB登录");
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    [self loginWithType:snsPlatform];
}
#pragma mark --- 所有第三方登录登录 ---
-(void)loginWithType:(UMSocialSnsPlatform *)snsPlatform
{
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            NSDictionary *thirdPlatformUserProfile = [response valueForKey:@"thirdPlatformUserProfile"];
            NSString *nickName = [thirdPlatformUserProfile objectForKey:@"nickname"];
            
            NSString *usid = [NSString string];
            NSString *iconImg = [NSString string];
            NSString *type = [NSString string];
            
            NSDictionary *data = [response valueForKey:@"data"];
            
            if ([data valueForKey:@"qq"]) {
                type = @"qq";
                usid = [[data valueForKey:@"qq"] objectForKey:@"usid"];
                iconImg = [thirdPlatformUserProfile objectForKey:@"figureurl_qq_2"];
            }
            if ([data valueForKey:@"wxsession"]) {
                type = @"wx";
                usid = [[data valueForKey:@"wxsession"] valueForKey:@"usid"];
                iconImg = [thirdPlatformUserProfile objectForKey:@"headimgurl"];
            }
            [self loginUserWithType:type userId:usid iconImg:iconImg nickName:[nickName stringByReplacingEmojiUnicodeWithCheatCodes]];
        }
    });
    
}
#pragma mark --- 获取的第三方的数据后发送给后台进行注册登录 ---
-(void)loginUserWithType:(NSString *)type userId:(NSString *)usid iconImg:(NSString *)iconImg nickName:(NSString *)nickname
{
    [[ZJUserCenter shareCenter] LoginUserWithType:type withUsid:usid withIconImg:iconImg withNickName:nickname withSuccess:^(NSString *message) {
        ZJToast(@"登录成功");
        MJRefreshLog(@"Success:%@",message);
    } withFaild:^(NSError *error) {
        ZJToast(@"登录失败");
        MJRefreshLog(@"Error:%@",error);
    }];
}
#pragma mark --- 退出键盘 ---
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.view endEditing:YES];
    return YES;
}
#pragma mark --- 状态栏 ---
-(void)createSta
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(staBarHeight);
    }];
}
#pragma mark --- 导航栏 ---
-(void)createNav
{
    self.navView = [[NavView alloc] initWithFrame:CGRectZero];
    self.navView.backgroundColor = [UIColor whiteColor];
    self.navView.titleString = @"登录";
    self.navView.leftImgNameStr = @"backblack";
    self.navView.rightString = @"注册";
    [self.navView setupInit];
    
    __weak typeof(self) weakSelf = self;
    self.navView.rightBlock = ^(){
        RegistVC *regist = [[RegistVC alloc] init];
        regist.registTimer = weakSelf.registTimerStr;
        [weakSelf presentViewController:regist animated:YES completion:^{
            
        }];
    };
    self.navView.leftBlock = ^(){
        [weakSelf dismissViewControllerAnimated:YES completion:^{
            
        }];
    };
    [self.view addSubview:self.navView];
}
#pragma mark --- 创建用户名和密码 ---
-(void)createUserAndPwd
{
    UIView *txtUserAndPwdView = [[UIView alloc] init];
    txtUserAndPwdView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:txtUserAndPwdView];
    //线条
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = SeparatorColor;
    [txtUserAndPwdView addSubview:lineView];
    //手机和密码
    self.txtUser = [[UITextField alloc] initWithFrame:CGRectZero];
    self.txtUser.placeholder = @"请输入您的手机号码";
    self.txtUser.delegate = self;
    self.txtUser.keyboardType = UIKeyboardTypeNumberPad;
    self.txtUser.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36, TextFieldLoginHeight)];
    self.txtUser.leftViewMode = UITextFieldViewModeAlways;
    UIImageView *imgUser = [[UIImageView alloc] init];
    imgUser.image = [UIImage imageNamed:@"user_phone"];
    imgUser.frame = CGRectMake(Space, TextFieldLoginHeight/2 - imgUser.image.size.height/2, imgUser.image.size.width, imgUser.image.size.height);
    [self.txtUser.leftView addSubview:imgUser];
    [txtUserAndPwdView addSubview:self.txtUser];
    
    self.txtPwd = [[UITextField alloc] initWithFrame:CGRectZero];
    self.txtPwd.placeholder = @"请输入您的密码";
    self.txtPwd.delegate = self;
    self.txtPwd.secureTextEntry = YES;
    self.txtPwd.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36, TextFieldLoginHeight)];
    self.txtPwd.leftViewMode = UITextFieldViewModeAlways;
    UIImageView *imgPwd = [[UIImageView alloc] init];
    imgPwd.image = [UIImage imageNamed:@"user_pwd"];
    imgPwd.frame = CGRectMake(Space, TextFieldLoginHeight/2 - imgPwd.image.size.height/2, imgPwd.image.size.width, imgPwd.image.size.height);
    [self.txtPwd.leftView addSubview:imgPwd];
    self.txtPwd.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36, 36)];
    self.txtPwd.rightViewMode = UITextFieldViewModeAlways;
    UIButton* findBtn = [[UIButton alloc] initWithFrame:CGRectMake(7, 7, 22, 22)];
    [findBtn setImage:[UIImage imageNamed:@"user_findpwd"] forState:UIControlStateNormal];
    [findBtn addTarget:self action:@selector(findPwd) forControlEvents:UIControlEventTouchUpInside];
    [self.txtPwd.rightView addSubview:findBtn];
    [txtUserAndPwdView addSubview:self.txtPwd];
    

    [txtUserAndPwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.navView.mas_bottom).offset(Space);
        make.left.and.right.mas_equalTo(0);
        make.bottom.mas_equalTo(self.txtPwd.mas_bottom);
    }];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(txtUserAndPwdView.center);
        make.left.mas_equalTo(txtUserAndPwdView.mas_left).offset(Margin);
        make.right.mas_equalTo(txtUserAndPwdView.mas_right).offset(-Margin);
        make.height.mas_equalTo(1);
    }];
    [self UserMakeConstraints];
}
#pragma mark --- 用户名和密码页面布局 ---
-(void)UserMakeConstraints
{
    [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset(staBarHeight);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(navBarHeight);
    }];
    
    [self.txtUser mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.navView.mas_bottom).offset(Space);
        make.left.mas_equalTo(self.view).offset(Margin);
        make.right.mas_equalTo(self.view).offset(-Margin);
        make.height.mas_equalTo(TextFieldLoginHeight);
    }];
    [self.txtPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.txtUser.mas_bottom);
        make.left.and.right.equalTo(self.txtUser);
        make.height.equalTo(self.txtUser.mas_height);
    }];

}

#pragma mark --- 创建登录按钮 ---
-(void)createLoginBtn
{
    self.login = [[UIButton alloc] init];
    self.login.layer.cornerRadius = 5;
    self.login.layer.masksToBounds = YES;
    [self.login addTarget:self action:@selector(userWantLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.login setTitle:@"登录" forState:UIControlStateNormal];
    [self.login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.login.backgroundColor = navBarBackgroundColor;
    [self.view addSubview:self.login];
    [self PhoneLoginMakeConstraints];
}
#pragma mark --- 用户登录按钮页面布局 ---
-(void)PhoneLoginMakeConstraints
{
    [self.login mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.txtPwd.mas_bottom).offset(2*Space);
        make.left.mas_equalTo(Space);
        make.right.mas_equalTo(-Space);
        make.height.mas_equalTo(TextFieldLoginHeight);
    }];
}

#pragma mark --- 第三方登录UI ---
-(void)createThirdLogin
{
    UIView *thirdView = [[UIView alloc] init];
    thirdView.backgroundColor = BackGroundColor;
    [self.view addSubview:thirdView];
    [thirdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.and.bottom.mas_equalTo(self.view);
        make.top.greaterThanOrEqualTo(self.login.mas_bottom).offset(20);
    }];
    //线条
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = SeparatorColor;
    [thirdView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(thirdView.mas_top).offset(14.5);
        make.left.mas_equalTo(Margin);
        make.right.mas_equalTo(-Margin);
        make.height.mas_equalTo(1);
    }];
    //文字
    UILabel *label = [[UILabel alloc] init];
    label.text = @" 第三方登录 ";
    label.backgroundColor = BackGroundColor;
    [thirdView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(thirdView.mas_top);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(30);
    }];
    //QQ&微信&微博
    UIButton *QQBtn = [[UIButton alloc] init];
    UIImage *QQImg = [UIImage imageNamed:@"user_QQ"];
    [QQBtn setImage:QQImg forState:UIControlStateNormal];
    [QQBtn setTitle:@"QQ" forState:UIControlStateNormal];
    [QQBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 15 + Space, 0)];
    [QQBtn setTitleEdgeInsets:UIEdgeInsetsMake(QQImg.size.height + Space, - QQImg.size.width, 0, 0)];
    [QQBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    QQBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    QQBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    if (CGSizeEqualToSize((CGSize){320,480},[UIScreen mainScreen].bounds.size)||CGSizeEqualToSize((CGSize){320,568},[UIScreen mainScreen].bounds.size)) {
//        [QQBtn setTitleEdgeInsets:UIEdgeInsetsMake(QQImg.size.height + Space, -QQImg.size.width*1.2, 0, 0)];
    }
    [QQBtn addTarget:self action:@selector(QQLogin) forControlEvents:UIControlEventTouchUpInside];
    
    //QQ&微信&微博
    UIButton *WXBtn = [[UIButton alloc] init];
    UIImage *WXImg = [UIImage imageNamed:@"user_WX"];
    [WXBtn setImage:WXImg forState:UIControlStateNormal];
    [WXBtn setTitle:@"微信" forState:UIControlStateNormal];
    [WXBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 15 + Space, 0)];
    [WXBtn setTitleEdgeInsets:UIEdgeInsetsMake(WXImg.size.height + Space, - WXImg.size.width, 0, 0)];
    [WXBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    WXBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    WXBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    if (CGSizeEqualToSize((CGSize){320,480},[UIScreen mainScreen].bounds.size)||CGSizeEqualToSize((CGSize){320,568},[UIScreen mainScreen].bounds.size)) {
//        [WXBtn setTitleEdgeInsets:UIEdgeInsetsMake(80, -50*1.2, 0, 0)];
    }
    [WXBtn addTarget:self action:@selector(WXLogin) forControlEvents:UIControlEventTouchUpInside];
    
    //QQ&微信&微博
    UIButton *WBBtn = [[UIButton alloc] init];
    UIImage *WBImg = [UIImage imageNamed:@"user_WX"];
    [WBBtn setImage:WBImg forState:UIControlStateNormal];
    [WBBtn setTitle:@"微博" forState:UIControlStateNormal];
    [WBBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 15 + Space, 0)];
    [WBBtn setTitleEdgeInsets:UIEdgeInsetsMake(WBImg.size.height + Space, - WBImg.size.width, 0, 0)];
    [WBBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    WBBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    WBBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    if (CGSizeEqualToSize((CGSize){320,480},[UIScreen mainScreen].bounds.size)||CGSizeEqualToSize((CGSize){320,568},[UIScreen mainScreen].bounds.size)) {
        //        [WXBtn setTitleEdgeInsets:UIEdgeInsetsMake(80, -50*1.2, 0, 0)];
    }
    [WBBtn addTarget:self action:@selector(WBLogin) forControlEvents:UIControlEventTouchUpInside];

    //自动布局
    int padding = (self.view.width - QQImg.size.width - WXImg.size.width - WBImg.size.width)/4;
    [thirdView addSubview:QQBtn];
    [QQBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(label.mas_bottom).offset(Space);
        make.left.mas_equalTo(thirdView.left).offset(padding);
        make.width.mas_equalTo(QQImg.size.width);
        make.height.mas_equalTo(QQImg.size.height + Space + 15);
    }];
    
    [thirdView addSubview:WXBtn];
    [WXBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(QQBtn.mas_top);
        make.left.mas_equalTo(QQBtn.mas_right).offset(padding);
        make.width.mas_equalTo(QQImg.size.width);
        make.height.mas_equalTo(QQImg.size.height + Space + 15);
    }];
    [thirdView addSubview:WBBtn];
    [WBBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(QQBtn.mas_top);
        make.left.mas_equalTo(WXBtn.mas_right).offset(padding);
        make.width.mas_equalTo(QQImg.size.width);
        make.height.mas_equalTo(QQImg.size.height + Space + 15);
    }];
    
}

-(void)addNotification
{
    [GJCFNotificationCenter addObserver:self selector:@selector(getRegistNowTimer:) name:RegistNowTimer object:nil];
    [GJCFNotificationCenter addObserver:self selector:@selector(getFindPwdNowTimer:) name:FindPWDNowTimer object:nil];
    self.registTimerStr = @"0";
    self.findPwdTimerStr = @"0";
}
-(void)getRegistNowTimer:(NSNotificationCenter *)sender
{
    NSNumber *i = [[sender valueForKey:@"userInfo"] valueForKey:@"registTime"];
    self.registTimerStr = [NSString stringWithFormat:@"%@",i];
}
-(void)getFindPwdNowTimer:(NSNotificationCenter *)sender
{
    NSNumber *i = [[sender valueForKey:@"userInfo"] valueForKey:@"findPWDTime"];
    self.findPwdTimerStr = [NSString stringWithFormat:@"%@",i];
}

-(void)dealloc
{
    [GJCFNotificationCenter removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
